import bisect
import collections


class Solution:
    def shortestWay(self, source: str, target: str) -> int:
        alphabets = set(source)
        for ch in target:
            if ch not in alphabets:
                return -1

        index = collections.defaultdict(list)
        for i, ch in enumerate(source):
            index[ch].append(i)

        ans = 1
        i = -1
        for ch in target:
            j = bisect.bisect_right(index[ch], i)
            if j == len(index[ch]):
                j = 0
                ans += 1
            i = index[ch][j]

        return ans


if __name__ == "__main__":
    print(Solution().shortestWay(source="abc", target="abcbc"))  # 2
    print(Solution().shortestWay(source="abc", target="acdbc"))  # -1
    print(Solution().shortestWay(source="xyz", target="xzyxz"))  # 3
